/**
 * 
 */
package com.huilian.security.app.authentication;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.huilian.iotbox.data.enums.LogLoginStatusEnum;
import com.huilian.iotbox.data.po.LogLogin;
import com.huilian.iotbox.data.service.LogLoginMQService;
import com.huilian.iotbox.data.utils.Util;
import com.huilian.security.core.common.ServerResponse;
import com.huilian.security.core.properties.LoginResponseType;
import com.huilian.security.core.properties.SecurityProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

/**
 * @author: 少凡
 * @Date 2018/1/25
 *
 */
@Slf4j
@Component("huiLianAuthenctiationFailureHandler")
public class HuiLianAuthenctiationFailureHandler extends SimpleUrlAuthenticationFailureHandler {

	@Autowired
	private ObjectMapper objectMapper;
	
	@Autowired
	private SecurityProperties securityProperties;

	@Autowired
	private LogLoginMQService logLoginMQService;
	
	/* (non-Javadoc)
	 * @see org.springframework.security.web.authentication.AuthenticationFailureHandler#onAuthenticationFailure(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.springframework.security.core.AuthenticationException)
	 */
	@Override
	public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
                                        AuthenticationException exception) throws IOException, ServletException {

		try {
			// 登录日志
			LogLogin logLogin = new LogLogin();
			logLogin.setUserName(request.getParameter("username"));
			logLogin.setIp(Util.getIpAddress(request));
//			logLogin.setPlace(Util.getCityInfo(logLogin.getIp()));
			Map<String, String> map = Util.getDevice(request.getHeader("User-Agent"));
			logLogin.setBrowser(map.get("browser"));
			logLogin.setSystem(map.get("system"));
			logLogin.setCreateTime(new Date());
			logLogin.setRemark("用户登录失败原因：" + exception.getMessage());
			logLogin.setStatus(LogLoginStatusEnum.FAIL.getCode());
			logLoginMQService.sendLogLoginToMQ(logLogin);
		} catch (Exception e){
			log.error("---------登录失败，MQ失败---------" + e);
		}
		log.error("登录失败:{}",exception.getMessage());
		
		if (LoginResponseType.JSON.equals(securityProperties.getBrowser().getLoginType())) {
			response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
			response.setContentType("application/json;charset=UTF-8");
			response.setHeader("Access-Control-Allow-Origin","*");
			response.getWriter().write(objectMapper.writeValueAsString(ServerResponse.createByError(exception.getMessage())));
		}else{
			super.onAuthenticationFailure(request, response, exception);
		}

	}

}
